package com.hfzy.ihk.web.linkcall.handlers.questionBase;

import com.alibaba.fastjson.JSON;
import com.hfzy.ihk.common.core.elasticsearch.ElasticsearchOperation;
import com.hfzy.ihk.common.core.elasticsearch.base.CommonTableOperationHandler;
import com.hfzy.ihk.facade.linkcall.entity.knowledge.QuestionBase;
import com.hfzy.ihk.facade.linkcall.enums.KnowledgeBase.QuestionBaseOperations;
import com.hfzy.ihk.facade.linkcall.enums.KnowledgeBase.QuestionBaseTables;
import com.hfzy.ihk.web.linkcall.biz.KnowledgeBase.QuestionBaseBiz;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by 欧怡聪 on 2018/8/14.
 *
 *
 * @author wws
 */
@Component
public class QuestionTablesQuestionUpdateOperationHandler extends CommonTableOperationHandler {

    private static final Logger logger = LoggerFactory.getLogger(QuestionTablesQuestionUpdateOperationHandler.class);

    @Autowired
    QuestionBaseBiz questionBaseBiz;

    public QuestionTablesQuestionUpdateOperationHandler() {

        /**
         * 必须定义该处理器对应的table和操作名
         * */
        this.table = QuestionBaseTables.QUESTIONBASE;

        this.tableOperation = QuestionBaseOperations.updateQuestionBase;
    }


    @Override
    protected void toHandle(ElasticsearchOperation elasticsearchOperation) {


        List data = elasticsearchOperation.getData();

        logger.debug("handle data:{}", JSON.toJSONString(data));

        if(data != null&&data.size()>0) {

            //强转一波
            List<QuestionBase> QuestionBases= (List<QuestionBase>)data.stream().map(d-> JSON.parseObject(JSON.toJSONString(d),QuestionBase.class)).collect(Collectors.toList());


            for (QuestionBase base : QuestionBases) {

                //调用biz更新学习状态的方法
                questionBaseBiz.esUpdateByFiledForStatus(base);
            }


        }

    }

}
